package com.ruoyi.basebill.task.util;

import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.mapper.task.OmsTaskUtilMapper;
import com.ruoyi.basebill.service.impl.OmsPurinwarehsbillServiceImpl;
import com.ruoyi.basebill.service.impl.OmsSaleissuebillServiceImpl;
import com.ruoyi.basebill.service.impl.OmsStocktransferbillServiceImpl;
import com.ruoyi.basedata.domain.*;
import com.ruoyi.basedata.service.impl.*;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;


@Component
public class OmsTaskUtil {

    //oms定时任务mapper层接口
    @Autowired
    private OmsTaskUtilMapper omsTaskutilMapper;

    //仓库服务层接口
    @Autowired
    private BdWarehouseServiceImpl warehouseService;

    //结算组织服务层接口
    @Autowired
    private OmsOrgunitServiceImpl orgunitService;

    //销售出库单服务层接口
    @Autowired
    private OmsSaleissuebillServiceImpl saleissuebillService;

    //采购入库单服务层接口
    @Autowired
    private OmsPurinwarehsbillServiceImpl purinwarehsbillService;

    //库存调拨单服务层接口
    @Autowired
    private OmsStocktransferbillServiceImpl stocktransferbillService;

    //客户基础资料服务层接口
    @Autowired
    private BdCustomerServiceImpl customerService;

    //物料基础资料服务层接口
    @Autowired
    private BdMaterialServiceImpl materialService;

    //供应商基础资料服务层接口
    @Autowired
    private BdSupplierServiceImpl supplierService;

    //结算价服务层接口
    @Autowired
    private OmsSettlementpriceServiceImpl settlementpriceService;

    /**
     * 传入单据对象，为单据编码赋值
     * @param o 传入的单据对象
     */
    public String setBillNumber(Object o) {
        //准备一个集合用来存储单据表名
        HashMap<String, String> tableName = new HashMap<>();
        tableName.put("库存调拨", "t_oms_stocktransferbill");
        tableName.put("销售出库", "t_oms_SaleIssueBill");
        tableName.put("采购入库", "t_oms_PurInWarehsBill");
        tableName.put("汇总销出单","t_oms_SumSaleIssueBill");
        tableName.put("汇总采入单","t_oms_SumPurInWarehsBill");
        tableName.put("汇总库存调拨单","t_oms_SumStockTransferBill");
        tableName.put("其他入库","t_oms_OtherIssueBill");

        Class<?> Class = o.getClass();
        if (Class == OmsStocktransferbill.class) {
            String s = tableName.get("库存调拨");
            //传入单据标识和数据库中最新的一条单据编号生成单据编号
            return createBillNumber("KCDB", ((OmsStocktransferbill) o).getBizdate(), s);
        } else if (Class == OmsSaleissuebill.class) {
            String s = tableName.get("销售出库");
            return createBillNumber("XXCK", ((OmsSaleissuebill) o).getBizdate(), s);
        } else if (Class == OmsPurinwarehsbill.class) {
            String s = tableName.get("采购入库");
            return createBillNumber("CGRK", ((OmsPurinwarehsbill) o).getBizdate(), s);
        }else if (Class == OmsSumsaleissuebill.class){
            String s = tableName.get("汇总销出单");
            return createBillNumber("HZXXCK",((OmsSumsaleissuebill) o).getBizdate(),s);
        }else if (Class == OmsSumpurinwarehsbill.class){
            String s = tableName.get("汇总采入单");
            return createBillNumber("HZCGRK",((OmsSumpurinwarehsbill)o).getBizdate(),s);
        }else if (Class == OmsSumstocktransferbill.class){
            String s = tableName.get("汇总库存调拨单");
            return createBillNumber("HZKCDB",((OmsSumstocktransferbill)o).getBizdate(),s);
        }else if (Class == OmsOtherissuebill.class){
            String s = tableName.get("其他入库");
            return createBillNumber("QTCK",((OmsOtherissuebill)o).getBizdate(),s);
        }
        return null;
    }


    /**
     *  根据传入的表名，生成对应的单据的编码
     * @param str 单据编号开头
     * @param date 业务日期
     * @param tableName 表名
     */
    private  String createBillNumber(String str, Date date, String tableName) {
        //时间字符串产生方式
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String format1 = format.format(date);
        String formerBillNumber;
        //获取指定单据的最新一条记录的编码
        if ("t_oms_SaleIssueBill".equals(tableName)){
            formerBillNumber = omsTaskutilMapper.getSaleNewNumber(format1);
        }else {
            formerBillNumber = omsTaskutilMapper.getNewNumber(format1, tableName);
        }

        //最终的单据编号
        String commentNumber = "";
        //组合流水号前一部分
        String billNumberPfix = format.format(date);
        //判断数据库中是否存在数据
        if (formerBillNumber != null && formerBillNumber.contains(billNumberPfix)) {
            //从单据编号中截取后6位
            String billNumberEnd = formerBillNumber.substring(formerBillNumber.length() - 6);
            int endNumber = Integer.parseInt(billNumberEnd);
            int tempNumber = 1000000 + endNumber + 1;
            //将首位的1去掉之后，拼接成流水号
            commentNumber = str + '-' + billNumberPfix + '-' + String.valueOf(tempNumber).substring(1);

        } else {
            //当数据库中没有数据时
            commentNumber = str + '-' + billNumberPfix + '-' + "000001";
        }

        return commentNumber;
    }

    /**
     * 对传入的收货确认单进行非空字段的校验
     * @param receiptbill 收货确认单
     * @param stringBuffer 错误信息
     * @return 结果
     */
    public int JudgeNullValues(OmsReceiptbill receiptbill,StringBuffer stringBuffer){
        boolean isEmpty = false;
        boolean isNull = false;
        String code = receiptbill.getCode();
        if (StringUtils.isEmpty(code)) {
            stringBuffer.append("[销售收货单号为空]");
            isEmpty = true;
        }
        String srcSys = receiptbill.getSrcsys();
        if (StringUtils.isEmpty(srcSys)) {
            stringBuffer.append("[来源系统为空]");
            isEmpty = true;
        }
        String shipmentcode = receiptbill.getShipmentcode();
        if (StringUtils.isEmpty(shipmentcode)) {
            stringBuffer.append("[发货单号为空]");
            isEmpty = true;
        }
        String customercode = receiptbill.getCustomercode();
        if (StringUtils.isEmpty(customercode)) {
            stringBuffer.append("[客户编码为空]");
            isEmpty = true;
        }
        String invoicecode = receiptbill.getInvoicecode();
        if (StringUtils.isEmpty(invoicecode)) {
            stringBuffer.append("[销售组织编码为空]");
            isEmpty = true;
        }
        String costcentercode = receiptbill.getCostcentercode();
        if (StringUtils.isEmpty(costcentercode)) {
            stringBuffer.append("[成本中心编码为空]");
            isEmpty = true;
        }
//        String saleswarehousecode = receiptbill.getSaleswarehousecode();
//        if (StringUtils.isEmpty(saleswarehousecode)) {
//            stringBuffer.append("[销售组织仓库编码为空]");
//            isEmpty = true;
//        }
        String inventoryorganizationcode = receiptbill.getInventoryorganizationcode();
        if (StringUtils.isEmpty(inventoryorganizationcode)) {
            stringBuffer.append("[库存组织编码为空]");
            isEmpty = true;
        }
        String facilitycode = receiptbill.getFacilitycode();
        if (StringUtils.isEmpty(facilitycode)) {
            stringBuffer.append("[发货仓库编码为空]");
            isEmpty = true;
        }
        Date receiptdate = receiptbill.getReceiptdate();
        if (StringUtils.isNull(receiptdate)) {
            stringBuffer.append("[收货日期为空]");
            isEmpty = true;
        }
        String currency = receiptbill.getCurrency();
        if (StringUtils.isEmpty(currency)) {
            stringBuffer.append("[币别为空]");
            isEmpty = true;
        }
        BigDecimal exchangerate = receiptbill.getExchangerate();
        if (StringUtils.isNull(exchangerate)) {
            stringBuffer.append("[汇率为空]");
            isEmpty = true;
        }

        if (isEmpty) {
            return 0;
        }

        BdCustomer bdCustomer = new BdCustomer();
        bdCustomer.setCustomernumber(customercode);
        List<BdCustomer> bdCustomers = customerService.selectBdCustomerList(bdCustomer);
        if (StringUtils.isEmpty(bdCustomers)) {
            stringBuffer.append("[没有找到客户：" + customercode + ']');
            isNull = true;
        }else {
            bdCustomer = bdCustomers.get(0);
            Long isinternal = bdCustomer.getIsinternal();
            Long iscostcenter = bdCustomer.getIscostcenter();
            if (isinternal.equals(1L) || iscostcenter.equals(1L)){
                //内部客户，则收货仓库需要校验
                String receivefacilitycode = receiptbill.getReceivefacilitycode();
                if (StringUtils.isEmpty(receivefacilitycode)){
                    stringBuffer.append("[收货仓库编码为空]");
                    isNull = true;
                }
            }
        }

        OmsOrgunit omsOrgunit = new OmsOrgunit();
        omsOrgunit.setOrgnumber(invoicecode);
        List<OmsOrgunit> omsOrgunits = orgunitService.selectOmsOrgunitList(omsOrgunit);
        if (StringUtils.isEmpty(omsOrgunits)) {
            stringBuffer.append("[没有找到销售组织：" + invoicecode + ']');
            isNull = true;
        }
        BdWarehouse bdWarehouse = new BdWarehouse();
//        bdWarehouse.setWarehousenumber(saleswarehousecode);
        List<BdWarehouse> bdWarehouses;
//        if (StringUtils.isEmpty(bdWarehouses)) {
//            stringBuffer.append("[没有找到仓库：" + saleswarehousecode + ']');
//            isNull = true;
//        }
        omsOrgunit.setOrgnumber(inventoryorganizationcode);
        omsOrgunits = orgunitService.selectOmsOrgunitList(omsOrgunit);
        if (StringUtils.isEmpty(omsOrgunits)) {
            stringBuffer.append("[没有找到库存组织：" + inventoryorganizationcode + ']');
            isNull = true;
        }
        bdWarehouse.setWarehousenumber(facilitycode);
        bdWarehouses = warehouseService.selectBdWarehouseList(bdWarehouse);
        if (StringUtils.isEmpty(bdWarehouses)) {
            stringBuffer.append("[没有找到发货仓库：" + facilitycode + ']');
            isNull = true;
        }
        if (isNull) {
            return 0;
        }
        //判断分录中是否符合要求
        List<OmsReceiptbillentry> omsReceiptbillentryList = receiptbill.getOmsReceiptbillentryList();
        if (StringUtils.isEmpty(omsReceiptbillentryList)) {
            stringBuffer.append("[分录为空！]");
            return 0;
        }
        int i = JudgeEntryNullValues(omsReceiptbillentryList, stringBuffer);
        if (i == 0) {
            return 0;
        }


        return 1;
    }

    /**
     * 对传入的收货确认单分录集合进行非空字段的校验
     * @param omsReceiptbillentryList 收货确认单分录
     * @param stringBuffer 错误信息
     * @return 结果
     */
    private int JudgeEntryNullValues(List<OmsReceiptbillentry> omsReceiptbillentryList, StringBuffer stringBuffer) {
        boolean isEmpty = false;
        for (int i = 0; i < omsReceiptbillentryList.size(); i++) {
            OmsReceiptbillentry omsReceiptbillentry = omsReceiptbillentryList.get(i);
            if (StringUtils.isEmpty(omsReceiptbillentry.getSkucode())) {
                stringBuffer.append("[第" + (i + 1) + "条分录" + "商品编号为空]");
                isEmpty = true;
            }
            if (StringUtils.isNull(omsReceiptbillentry.getReceivedqty())) {
                stringBuffer.append("[第" + (i + 1) + "条分录" + "实收数量为空]");
                isEmpty = true;
            }
            if (StringUtils.isEmpty(omsReceiptbillentry.getUnit())) {
                stringBuffer.append("[第" + (i + 1) + "条分录" + "计量单位编码为空]");
                isEmpty = true;
            }
            if (StringUtils.isNull(omsReceiptbillentry.getBasereceivedqty())) {
                stringBuffer.append("[第" + (i + 1) + "条分录" + "基本实收数量为空]");
                isEmpty = true;
            }
            if (StringUtils.isEmpty(omsReceiptbillentry.getBaseunit())) {
                stringBuffer.append("[第" + (i + 1) + "条分录" + "基本计量单位为空]");
                isEmpty = true;
            }
            if (StringUtils.isNull(omsReceiptbillentry.getNotaxprice())) {
                stringBuffer.append("[第" + (i + 1) + "条分录" + "不含税单价为空]");
                isEmpty = true;
            }
            if (StringUtils.isNull(omsReceiptbillentry.getNotaxamount())) {
                stringBuffer.append("[第" + (i + 1) + "条分录" + "不含税总金额为空]");
                isEmpty = true;
            }
            if (StringUtils.isNull(omsReceiptbillentry.getTaxprice())) {
                stringBuffer.append("[第" + (i + 1) + "条分录" + "含税单价为空]");
                isEmpty = true;
            }
            if (StringUtils.isNull(omsReceiptbillentry.getTaxamount())) {
                stringBuffer.append("[第" + (i + 1) + "条分录" + "含税总金额为空]");
                isEmpty = true;
            }
            if (StringUtils.isNull(omsReceiptbillentry.getIsgift())) {
                stringBuffer.append("[第" + (i + 1) + "条分录" + "是否赠品为空]");
                isEmpty = true;
            }
        }
        if (isEmpty) {
            return 0;
        }
        return 1;
    }


    /**
     * 传入收货确认单，生成一张对外的销售出库单
     * @param omsReceiptbill 收货确认单对象
     * @param stringBuffer   存储错误信息
     * @return 生成结果
     */
    public int createForeignSaleIssueBill(OmsReceiptbill omsReceiptbill, StringBuffer stringBuffer) {
        OmsSaleissuebill omsSaleissuebill = new OmsSaleissuebill();
        ArrayList<OmsSaleissuebillentry> saleissuebillentries = new ArrayList<>();
        for (OmsReceiptbillentry omsReceiptbillentry : omsReceiptbill.getOmsReceiptbillentryList()) {
            OmsSaleissuebillentry omsSaleissuebillentry = new OmsSaleissuebillentry();
            //获取仓库编码
            String foreignWareHouse = getForeignWareHouse(omsReceiptbill, stringBuffer);
            if (StringUtils.isEmpty(foreignWareHouse)) {
                return 0;
            }

            BdWarehouse bdWarehouse = new BdWarehouse();
            bdWarehouse.setWarehousenumber(foreignWareHouse);
            List<BdWarehouse> bdWarehouses = warehouseService.selectBdWarehouseList(bdWarehouse);
            if (StringUtils.isEmpty(bdWarehouses)) {
                stringBuffer.append("没有找到仓库：" + foreignWareHouse);
                return 0;
            }
            bdWarehouse = bdWarehouses.get(0);
            //仓库编码
            omsSaleissuebillentry.setWarehousenumber(foreignWareHouse);
            //仓库名称
            omsSaleissuebillentry.setWarehousename(bdWarehouse.getWarehousename());
            //物料编码
            omsSaleissuebillentry.setMaterialnumber(omsReceiptbillentry.getSkucode());
            //物料名称
            omsSaleissuebillentry.setMaterialname(omsReceiptbillentry.getSkuname());
            //计量单位
            omsSaleissuebillentry.setUnitnumber(omsReceiptbillentry.getUnit());
            //基本计量单位
            omsSaleissuebillentry.setBaseunitnumber(omsReceiptbillentry.getBaseunit());
            //数量
            omsSaleissuebillentry.setQty(omsReceiptbillentry.getReceivedqty());
            //基本单位数量
            omsSaleissuebillentry.setBaseqty(omsReceiptbillentry.getBasereceivedqty());
            //单价
            omsSaleissuebillentry.setPrice(omsReceiptbillentry.getNotaxprice());
            //税率
            omsSaleissuebillentry.setTaxrate(omsReceiptbillentry.getTaxrate());
            //系统含税单价
            omsSaleissuebillentry.setSystaxprice(omsReceiptbillentry.getSystaxprice());
            //含税单价
            omsSaleissuebillentry.setTaxprice(omsReceiptbillentry.getTaxprice());
            //金额
            omsSaleissuebillentry.setAmount(omsReceiptbillentry.getNotaxamount());
            //税额
            omsSaleissuebillentry.setTax(omsReceiptbillentry.getTax());
            //价税合计
            omsSaleissuebillentry.setTaxamount(omsReceiptbillentry.getTaxamount());
            //是否赠品
            omsSaleissuebillentry.setIspresent(omsReceiptbillentry.getIsgift());
            //客户订单号
            omsSaleissuebillentry.setCustomerOrderNo(omsReceiptbillentry.getCustomerorder());
            //销售订单号
            omsSaleissuebillentry.setRefordercode(omsReceiptbillentry.getRefordercode());
            //备注数据
            omsSaleissuebillentry.setRemark(omsReceiptbillentry.getRemark());
            saleissuebillentries.add(omsSaleissuebillentry);

        }
        //业务日期
        omsSaleissuebill.setBizdate(omsReceiptbill.getReceiptdate());
        //对外销出单编号就是原单编号
        //单据编号
        omsSaleissuebill.setBillnumber(omsReceiptbill.getCode());
        //库存组织编码
        omsSaleissuebill.setStorageorgnumber(omsReceiptbill.getInvoicecode());
        //库存组织名称
        omsSaleissuebill.setStorageorgname(omsReceiptbill.getInvoicename());
        //业务类型编码
        omsSaleissuebill.setBiztypenumber("210");
        //业务类型名称
        omsSaleissuebill.setBiztypename("普通销售");
        //事务类型编码
        omsSaleissuebill.setTransactiontypenumber("010");
        //事务类型名称
        omsSaleissuebill.setTransactiontypename("普通销售出库");
        //送货客户编码
        String customercode = omsReceiptbill.getCustomercode();
        omsSaleissuebill.setCustomernumber(customercode);
        //送货客户名称
        omsSaleissuebill.setCustomername(omsReceiptbill.getCustomername());
        //修改获取记账公司的逻辑，
        if ("200043".equals(customercode)){
            BdCustomer bdCustomer = new BdCustomer();
            bdCustomer.setCustomernumber(customercode);
            List<BdCustomer> bdCustomers = customerService.selectBdCustomerList(bdCustomer);
            if (StringUtils.isEmpty(bdCustomers)){
                stringBuffer.append("没有找到客户：" + customercode);
                return 0;
            }
            bdCustomer = bdCustomers.get(0);
            String accountcompanynumber = bdCustomer.getAccountcompanynumber();
            String accountcompanyname = bdCustomer.getAccountcompanyname();
            //设置记账公司数据
            omsSaleissuebill.setAccountcompanynumber(accountcompanynumber);
            omsSaleissuebill.setAccountcompanyname(accountcompanyname);
        }else {
        //记账公司编码
        omsSaleissuebill.setAccountcompanynumber(omsReceiptbill.getInvoicecode());
        //记账公司名称
        omsSaleissuebill.setAccountcompanyname(omsReceiptbill.getInvoicename());
        }
        //成本中心编码
        omsSaleissuebill.setCostcenternumber(omsReceiptbill.getCostcentercode());
        //成本中心名称
        omsSaleissuebill.setCostcentername(omsReceiptbill.getCostcentername());
        //币别
        omsSaleissuebill.setCurrency(omsReceiptbill.getCurrency());
        //汇率
        omsSaleissuebill.setExchangerate(omsReceiptbill.getExchangerate());
        //厂编
        omsSaleissuebill.setFactorymake(omsReceiptbill.getFactorymake());
        //来源单据类型
        omsSaleissuebill.setSrcbilltype("收货确认");
        //来源单据编号
        omsSaleissuebill.setSrcbillnumber(omsReceiptbill.getCode());
        //备注
        omsSaleissuebill.setDescription(omsReceiptbill.getBuyernote());
        //创建时间
        omsSaleissuebill.setCreatetime(new Date());
        //是否外部客户
        omsSaleissuebill.setIsoutcustomer(1l);
        //销售订单类型
        omsSaleissuebill.setInternalinstructiontype(omsReceiptbill.getInternalinstructiontype());
        //来源系统
        omsSaleissuebill.setSrcsys(omsReceiptbill.getSrcsys());
        //设置分录数据
        omsSaleissuebill.setOmsSaleissuebillentryList(saleissuebillentries);

        return saleissuebillService.insertOmsSaleissuebill(omsSaleissuebill);
    }

    /**
     * 供 生成对外的销售出库单使用
     * 传入收货确认单对象，获取分录仓库编码数据
     * @param omsReceiptbill 收货确认单
     * @param stringBuffer 错误信息
     * @return 仓库编码
     */
    public String getForeignWareHouse(OmsReceiptbill omsReceiptbill, StringBuffer stringBuffer) {
//        /**
//         * 1、如果来源系统是wdt，则取传过来的发货仓facilityCode。
//         * 2、如果销售组织编码=库存组织编码：发货仓对应的在途仓，没有在途仓就取发货仓；
//         * 3、否则，根据销售组织编码invoiceCode取结算组织基础资料，结算组织的中转仓如果不为空，则取中转仓，否则取传过来的销售组织仓库编码（对应EAS客户的分公司门店仓）
//         */
        /**
         * 1.如果销售组织 = 库存组织，并且来源系统是wdt，就直接取发货仓，其他情况，发货仓有在途仓取在途，没有则取发货仓
         * 2.销售组织 ！= 库存组织，根据销售组织编码invoiceCode取结算组织基础资料，结算组织的中转仓如果不为空，则取中转仓，否则取传过来的销售组织仓库编码（对应EAS客户的分公司门店仓）
         */
        String srcSys = omsReceiptbill.getSrcsys();
        String invoicecode = omsReceiptbill.getInvoicecode();
        String inventoryorganizationcode = omsReceiptbill.getInventoryorganizationcode();
        if (invoicecode.equals(inventoryorganizationcode)) {
            BdWarehouse bdWarehouse = new BdWarehouse();
            String facilitycode = omsReceiptbill.getFacilitycode();
            if ("wdt".equals(srcSys)) {
                return facilitycode;
            }else {
                bdWarehouse.setWarehousenumber(facilitycode);
                List<BdWarehouse> bdWarehouses = warehouseService.selectBdWarehouseList(bdWarehouse);
                bdWarehouse = bdWarehouses.get(0);
                String transferwhnumber = bdWarehouse.getTransferwhnumber();
                if (StringUtils.isEmpty(transferwhnumber)) {
                    return facilitycode;
                } else {
                    return transferwhnumber;
                }
            }
        }
        OmsOrgunit omsOrgunit = new OmsOrgunit();
        omsOrgunit.setOrgnumber(invoicecode);
        List<OmsOrgunit> omsOrgunits = orgunitService.selectOmsOrgunitList(omsOrgunit);
        omsOrgunit = omsOrgunits.get(0);
        String transferwhnumber = omsOrgunit.getTransferwhnumber();
        if (StringUtils.isEmpty(transferwhnumber)) {
            String saleswarehousecode = omsReceiptbill.getSaleswarehousecode();
            if (StringUtils.isEmpty(saleswarehousecode)){
                stringBuffer.append("[生成对外销出单时，单据头销售组织仓库编码为空！]");
                return null;
            }
            return saleswarehousecode;
        } else {
            return transferwhnumber;
        }

    }


    /**
     * 传入库存组织、客户、收货确认单 生成一张库存调拨单
     * @param inventoryUnit 库存组织
     * @param bdCustomer 客户
     * @param receiptbill 收货确认单
     * @param stringBuffer 错误信息
     * @return 生成结果
     */
    public int createFirstOrgSTBill(OmsOrgunit inventoryUnit, BdCustomer bdCustomer, OmsReceiptbill receiptbill, StringBuffer stringBuffer) {
        OmsStocktransferbill omsStocktransferbill = new OmsStocktransferbill();
        // 开始设置单据头数据

        //业务日期
        omsStocktransferbill.setBizdate(receiptbill.getReceiptdate());
        //单据编号
        String billNumber = setBillNumber(omsStocktransferbill);
        omsStocktransferbill.setBillnumber(billNumber);
        //业务类型
        omsStocktransferbill.setBiztypenumber("331");
        omsStocktransferbill.setBiztypename("跨仓库调拨（成本价）");
        //获取库存组织
        String orgnumber = inventoryUnit.getOrgnumber();
        String orgname = inventoryUnit.getOrgname();
        //调出仓库组织
        omsStocktransferbill.setIssuestorageorgnumber(orgnumber);
        omsStocktransferbill.setIssuestorageorgname(orgname);
        //调入库存组织
        omsStocktransferbill.setReceiptstorageorgnumber(orgnumber);
        omsStocktransferbill.setReceiptstorageorgname(orgname);
        //调出成本中心
//        omsStocktransferbill.setOutcostcenternumber(receiptbill.getCostcentercode());
//        omsStocktransferbill.setOutcostcentername(receiptbill.getCostcentername());
        //调出记账公司
//        omsStocktransferbill.setOutaccountcompanynumber(orgnumber);
//        omsStocktransferbill.setOutaccountcompanyname(orgname);
        //调入成本中心
//        String costcenternumber = bdCustomer.getCostcenternumber();
//        if (StringUtils.isEmpty(costcenternumber)) {
//            stringBuffer.append("[客户：" + bdCustomer.getCustomernumber() + " 的成本中心为空！]");
//            return 0;
//        }
//        omsStocktransferbill.setIncostcenternumber(costcenternumber);
//        omsStocktransferbill.setIncostcentername(bdCustomer.getCostcentername());
        //调入记账公司
//        omsStocktransferbill.setInaccountcompanynumber(orgnumber);
//        omsStocktransferbill.setInaccountcompanyname(orgname);
        //币别
        omsStocktransferbill.setCurrency(receiptbill.getCurrency());
        //汇率
        omsStocktransferbill.setExchangerate(receiptbill.getExchangerate());
        //来源单据
        omsStocktransferbill.setSrcbilltype("收货确认");
        omsStocktransferbill.setSrcbillnumber(receiptbill.getCode());
        //备注
        omsStocktransferbill.setDescription(receiptbill.getBuyernote());
        //创建时间
        omsStocktransferbill.setCreatetime(new Date());

        //设置分录数据
        ArrayList<OmsStocktransferbillentry> stocktransferbillentries = new ArrayList<>();
        for (OmsReceiptbillentry omsReceiptbillentry : receiptbill.getOmsReceiptbillentryList()) {
            OmsStocktransferbillentry omsStocktransferbillentry = new OmsStocktransferbillentry();
            //物料
            omsStocktransferbillentry.setMaterialnumber(omsReceiptbillentry.getSkucode());
            omsStocktransferbillentry.setMaterialname(omsReceiptbillentry.getSkuname());
            //调出仓库，获取调出仓库的在途仓库
            String facilitycode = receiptbill.getFacilitycode();
            BdWarehouse bdWarehouse = new BdWarehouse();
            bdWarehouse.setWarehousenumber(facilitycode);
            List<BdWarehouse> bdWarehouses = warehouseService.selectBdWarehouseList(bdWarehouse);
            bdWarehouse = bdWarehouses.get(0);
            String transferwhnumber = bdWarehouse.getTransferwhnumber();
            if (StringUtils.isEmpty(transferwhnumber)){
                stringBuffer.append("[单据头发货仓库：" + facilitycode + "无在途仓]");
                return 0;
            }
            omsStocktransferbillentry.setIssuewarehousenumber(transferwhnumber);
            omsStocktransferbillentry.setIssuewarehousename(bdWarehouse.getTransferwhname());
            //调入仓库
            omsStocktransferbillentry.setReceiptwarehousenumber(receiptbill.getReceivefacilitycode());
            omsStocktransferbillentry.setReceiptwarehousename(receiptbill.getReceivefacilityname());
            //计量单位
            omsStocktransferbillentry.setUnitnumber(omsReceiptbillentry.getUnit());
            //基本计量单位
            omsStocktransferbillentry.setBaseunitnumber(omsReceiptbillentry.getBaseunit());
            //数量
            omsStocktransferbillentry.setQty(omsReceiptbillentry.getReceivedqty());
            //基本单位数量
            omsStocktransferbillentry.setBaseqty(omsReceiptbillentry.getBasereceivedqty());
            //计划调出日期
            omsStocktransferbillentry.setIssueplandate(receiptbill.getReceiptdate());
            //计划调入日期
            omsStocktransferbillentry.setReceiptplandate(receiptbill.getReceiptdate());
            //赠品
            omsStocktransferbillentry.setIspresent(omsReceiptbillentry.getIsgift());

            stocktransferbillentries.add(omsStocktransferbillentry);
        }
        omsStocktransferbill.setOmsStocktransferbillentryList(stocktransferbillentries);
        int i = stocktransferbillService.insertOmsStocktransferbill(omsStocktransferbill);
        if (i == 0) {
            stringBuffer.append("[插入库存调拨单时出现错误！]");
            return 0;
        }
        return 1;
    }


    /**
     * 传入收货确认单、库存组织、销售组织和客户，生成一级组织之间的销售出货单和采购入库单
     * @param inventoryUnit 库存组织
     * @param invoiceUnit   销售组织
     * @param receiptbill   收货确认单
     * @param customer      收货确认单中的客户
     * @param stringBuffer  错误信息
     * @return 生成结果
     */
    public int createFirstSIBillAndPWBill(OmsOrgunit inventoryUnit, OmsOrgunit invoiceUnit, OmsReceiptbill receiptbill, BdCustomer customer, StringBuffer stringBuffer) {
        OmsSaleissuebill omsSaleissuebill = new OmsSaleissuebill();
        OmsPurinwarehsbill omsPurinwarehsbill = new OmsPurinwarehsbill();
        /*设置销出单单据头数据*/


        //业务日期
        omsSaleissuebill.setBizdate(receiptbill.getReceiptdate());
        String billNumber = setBillNumber(omsSaleissuebill);
        //单据编号
        omsSaleissuebill.setBillnumber(billNumber);
        //库存组织
        omsSaleissuebill.setStorageorgnumber(receiptbill.getInventoryorganizationcode());
        omsSaleissuebill.setStorageorgname(receiptbill.getInventoryorganizationname());
        //业务类型
        omsSaleissuebill.setBiztypenumber("210");
        omsSaleissuebill.setBiztypename("普通销售");
        //事务类型
        omsSaleissuebill.setTransactiontypenumber("012");
        omsSaleissuebill.setTransactiontypename("成本出库");
        //获取送货客户
        String invoiceUnitNumber = invoiceUnit.getOrgnumber();
        BdCustomer bdCustomer = new BdCustomer();
        bdCustomer.setInternalcompanynumber(invoiceUnitNumber);
        List<BdCustomer> bdCustomers = customerService.selectBdCustomerList(bdCustomer);
        if (StringUtils.isEmpty(bdCustomers)) {
            stringBuffer.append("[没有找到内部公司编码为：" + invoiceUnitNumber + "的客户]");
            return 0;
        }
        bdCustomer = bdCustomers.get(0);
        //设置送货客户
        omsSaleissuebill.setCustomernumber(bdCustomer.getCustomernumber());
        omsSaleissuebill.setCustomername(bdCustomer.getCustomername());
        //记账公司
        omsSaleissuebill.setAccountcompanynumber(receiptbill.getInventoryorganizationcode());
        omsSaleissuebill.setAccountcompanyname(receiptbill.getInventoryorganizationname());
        //成本中心
        omsSaleissuebill.setCostcenternumber("1.91");
        omsSaleissuebill.setCostcentername("关联单位");
//        //币别
//        omsSaleissuebill.setCurrency("CNY");
        //币别获取逻辑修改为获取上游单据类型
        omsSaleissuebill.setCurrency(receiptbill.getCurrency());
        //汇率
        omsSaleissuebill.setExchangerate(new BigDecimal(1));

        //厂编
        omsSaleissuebill.setFactorymake(receiptbill.getFactorymake());
        //来源单据
        omsSaleissuebill.setSrcbilltype("收货确认");
        omsSaleissuebill.setSrcbillnumber(receiptbill.getCode());
        //备注
        omsSaleissuebill.setDescription(receiptbill.getBuyernote());
        //创建时间
        omsSaleissuebill.setCreatetime(new Date());
        //是否外部客户
        omsSaleissuebill.setIsoutcustomer(0l);


        /*设置采入单单据头数据*/


        //设置业务日期
        omsPurinwarehsbill.setBizdate(receiptbill.getReceiptdate());
        String PWbillNumber = setBillNumber(omsPurinwarehsbill);
        //设置单据编号
        omsPurinwarehsbill.setBillnumber(PWbillNumber);
        //设置库存组织
        omsPurinwarehsbill.setStorageorgnumber(invoiceUnitNumber);
        omsPurinwarehsbill.setStorageorgname(invoiceUnit.getOrgname());
        //设置业务类型
        omsPurinwarehsbill.setBiztypenumber("110");
        omsPurinwarehsbill.setBiztypename("普通采购");
        //设置事务类型
        omsPurinwarehsbill.setTransactiontypenumber("004");
        omsPurinwarehsbill.setTransactiontypename("成本入库");
        //获取发货组织对应的供应商
        String inventoryNumber = inventoryUnit.getOrgnumber();
        BdSupplier bdSupplier = new BdSupplier();
        bdSupplier.setInternalcompanynumber(inventoryNumber);
        List<BdSupplier> bdSuppliers = supplierService.selectBdSupplierList(bdSupplier);
        if (StringUtils.isEmpty(bdSuppliers)) {
            stringBuffer.append("[没有找到内部公司编码为：" + inventoryNumber + "的供应商]");
            return 0;
        }
        bdSupplier = bdSuppliers.get(0);
        //设置供应商
        omsPurinwarehsbill.setSuppliernumber(bdSupplier.getSuppliernumber());
        omsPurinwarehsbill.setSuppliername(bdSupplier.getSuppliername());
        //设置成本中心
        omsPurinwarehsbill.setCostcenternumber("1.91");
        omsPurinwarehsbill.setCostcentername("关联单位");
        //设置记账公司
        omsPurinwarehsbill.setAccountcompanynumber(invoiceUnitNumber);
        omsPurinwarehsbill.setAccountcompanyname(invoiceUnit.getOrgname());
        //币别
//        omsPurinwarehsbill.setCurrency("CNY");
        //币别修改获取逻辑
        omsPurinwarehsbill.setCurrency(receiptbill.getCurrency());
        //汇率
        omsPurinwarehsbill.setExchangerate(BigDecimal.valueOf(1));
        //来源单据
        omsPurinwarehsbill.setSrcbilltype("收货确认");
        omsPurinwarehsbill.setSrcbillnumber(receiptbill.getCode());
        //备注
        omsPurinwarehsbill.setDescription(receiptbill.getBuyernote());
        //创建时间
        omsPurinwarehsbill.setCreatetime(new Date());



        //设置分录数据
        ArrayList<OmsSaleissuebillentry> saleissuebillentries = new ArrayList<>();
        ArrayList<OmsPurinwarehsbillentry> purinwarehsbillentries = new ArrayList<>();
        //获取销售出库单的发货仓库
        String wareHouse;
        String wareHouseName;
        String facilitycode = receiptbill.getFacilitycode();
        BdWarehouse bdWarehouse = new BdWarehouse();
        bdWarehouse.setWarehousenumber(facilitycode);
        List<BdWarehouse> bdWarehouses = warehouseService.selectBdWarehouseList(bdWarehouse);
        bdWarehouse = bdWarehouses.get(0);
        String transferwhnumber = bdWarehouse.getTransferwhnumber();
        if (StringUtils.isEmpty(transferwhnumber)) {
            wareHouse = facilitycode;
            wareHouseName = bdWarehouse.getWarehousename();
        } else {
            wareHouse = transferwhnumber;
            wareHouseName = bdWarehouse.getTransferwhname();
        }
        //获取采购入库单的仓库数据
        String pWWareHouse;
        String pWWareHouseName;
        //1、如果单头storageOrgNumber等于传过来的销售组织，且客户为内部客户（内部客户=1或者成本中心=1），则取传过来的收货仓库。
        //2、否则，单头storageOrgNumber的中转仓

        //逻辑修改：直接获取单头storageOrgNumber的中转仓
            String transferwhnumber1 = invoiceUnit.getTransferwhnumber();
            if (StringUtils.isEmpty(transferwhnumber1)) {
                stringBuffer.append("[组织：" + invoiceUnitNumber + "的中转仓为空！]");
                return 0;
            }
            pWWareHouse = transferwhnumber1;
            pWWareHouseName = invoiceUnit.getTransferwhname();


        for (OmsReceiptbillentry omsReceiptbillentry : receiptbill.getOmsReceiptbillentryList()) {
            /*设置销出单分录数据*/


            OmsSaleissuebillentry omsSaleissuebillentry = new OmsSaleissuebillentry();
            //仓库
            omsSaleissuebillentry.setWarehousenumber(wareHouse);
            omsSaleissuebillentry.setWarehousename(wareHouseName);
            //物料
            String skucode = omsReceiptbillentry.getSkucode();
            String skuname = omsReceiptbillentry.getSkuname();
            omsSaleissuebillentry.setMaterialnumber(skucode);
            omsSaleissuebillentry.setMaterialname(skuname);
            //计量单位
            String unit = omsReceiptbillentry.getUnit();
            omsSaleissuebillentry.setUnitnumber(unit);
            //基本计量单位
            String baseunit = omsReceiptbillentry.getBaseunit();
            omsSaleissuebillentry.setBaseunitnumber(baseunit);
            //数量
            BigDecimal qty = omsReceiptbillentry.getReceivedqty();
            omsSaleissuebillentry.setQty(qty);
            //基本单位数量
            BigDecimal baseQty = omsReceiptbillentry.getBasereceivedqty();
            omsSaleissuebillentry.setBaseqty(baseQty);
            //系统含税单价
            BigDecimal systaxprice = omsReceiptbillentry.getSystaxprice();
            omsSaleissuebillentry.setSystaxprice(systaxprice);
            //含税单价
            BigDecimal taxPrice = getTaxPrice(inventoryUnit, omsSaleissuebill.getCustomernumber(), omsSaleissuebill.getBizdate(), omsSaleissuebillentry.getMaterialnumber(), stringBuffer);
            if (StringUtils.isNull(taxPrice)) {
                return 0;
            }
            omsSaleissuebillentry.setTaxprice(taxPrice);
            //税率
            BigDecimal tempTaxrate = omsReceiptbillentry.getTaxrate();
            omsSaleissuebillentry.setTaxrate(tempTaxrate);
            BigDecimal taxrate = tempTaxrate;
            if (StringUtils.isNull(tempTaxrate)) {
                taxrate = BigDecimal.ZERO;
            }
            BigDecimal tempTaxRate = OmsTaskUtil.formatTaxRate(taxrate);
            //单价
            BigDecimal price = taxPrice.divide(tempTaxRate.add(BigDecimal.valueOf(1)),6,BigDecimal.ROUND_HALF_UP);
            omsSaleissuebillentry.setPrice(price);
            //税价合计
            BigDecimal taxAmount = taxPrice.multiply(qty).setScale(6, RoundingMode.HALF_UP);
            omsSaleissuebillentry.setTaxamount(taxAmount);
            //金额
//            BigDecimal amount = taxAmount.divide(tempTaxRate.add(BigDecimal.valueOf(1)),2,BigDecimal.ROUND_HALF_UP);
            BigDecimal amount = price.multiply(qty).setScale(2, RoundingMode.HALF_UP);
            omsSaleissuebillentry.setAmount(amount);
            //税额
            BigDecimal tax = taxAmount.subtract(amount);
            omsSaleissuebillentry.setTax(tax);
            //是否赠品
            //内部销售没有赠品!
            Long isgift = omsReceiptbillentry.getIsgift();
            omsSaleissuebillentry.setIspresent(0L);
            //客户订单号
            omsSaleissuebillentry.setCustomerOrderNo(omsReceiptbillentry.getCustomerorder());

            saleissuebillentries.add(omsSaleissuebillentry);


            /*设置采入单分录数据*/
            OmsPurinwarehsbillentry omsPurinwarehsbillentry = new OmsPurinwarehsbillentry();
            //仓库
            omsPurinwarehsbillentry.setWarehousenumber(pWWareHouse);
            omsPurinwarehsbillentry.setWarehousename(pWWareHouseName);
            //物料
            omsPurinwarehsbillentry.setMaterialnumber(skucode);
            omsPurinwarehsbillentry.setMaterialname(skuname);
            //计量单位
            omsPurinwarehsbillentry.setUnitnumber(unit);
            omsPurinwarehsbillentry.setBaseunitnumber(baseunit);
            //数量
            omsPurinwarehsbillentry.setQty(qty);
            omsPurinwarehsbillentry.setBaseqty(baseQty);
            //含税单价
            omsPurinwarehsbillentry.setTaxprice(taxPrice);
            //税率
            omsPurinwarehsbillentry.setTaxrate(tempTaxrate);
            //单价
            omsPurinwarehsbillentry.setPrice(price);
            //税价合计
            omsPurinwarehsbillentry.setTaxamount(taxAmount);
            //金额
            omsPurinwarehsbillentry.setAmount(amount);
            //税额
            omsPurinwarehsbillentry.setTax(tax);
            //是否赠品
            //内部销售没有赠品!
            omsPurinwarehsbillentry.setIspresent(0L);

            purinwarehsbillentries.add(omsPurinwarehsbillentry);
        }
        omsSaleissuebill.setOmsSaleissuebillentryList(saleissuebillentries);
        omsPurinwarehsbill.setOmsPurinwarehsbillentryList(purinwarehsbillentries);

        int i = saleissuebillService.insertOmsSaleissuebill(omsSaleissuebill);
        if (i == 0) {
            stringBuffer.append("[插入销出单时出现错误！]");
            return 0;
        }
        int i1 = purinwarehsbillService.insertOmsPurinwarehsbill(omsPurinwarehsbill);
        if (i1 == 0) {
            stringBuffer.append("[插入采购入库单时出现错误！]");
            return 0;
        }

        return 1;
    }


    /**
     * 传入收货确认单、库存组织、销售组织，生成销售出货单和采购入库单
     * 生成中间过渡的销出采入单
     * @param inventoryUnit 库存组织
     * @param invoiceUnit   销售组织
     * @param receiptbill   收货确认单
     * @param customer      单据头的客户
     * @param stringBuffer  错误信息
     * @return 生成信息
     */
    public int createTransitionSIBillAndPWBill(OmsOrgunit inventoryUnit, OmsOrgunit invoiceUnit, OmsReceiptbill receiptbill, BdCustomer customer, StringBuffer stringBuffer) {
        OmsSaleissuebill omsSaleissuebill = new OmsSaleissuebill();
        OmsPurinwarehsbill omsPurinwarehsbill = new OmsPurinwarehsbill();
        /*设置销出单单据头数据*/

        //业务日期
        omsSaleissuebill.setBizdate(receiptbill.getReceiptdate());
        String billNumber = setBillNumber(omsSaleissuebill);
        //单据编号
        omsSaleissuebill.setBillnumber(billNumber);
        //库存组织
        omsSaleissuebill.setStorageorgnumber(inventoryUnit.getOrgnumber());
        omsSaleissuebill.setStorageorgname(inventoryUnit.getOrgname());
        //业务类型
        omsSaleissuebill.setBiztypenumber("210");
        omsSaleissuebill.setBiztypename("普通销售");
        //事务类型
        omsSaleissuebill.setTransactiontypenumber("012");
        omsSaleissuebill.setTransactiontypename("成本出库");
        //获取送货客户
        String invoiceUnitNumber = invoiceUnit.getOrgnumber();
        BdCustomer bdCustomer = new BdCustomer();
        bdCustomer.setInternalcompanynumber(invoiceUnitNumber);
        List<BdCustomer> bdCustomers = customerService.selectBdCustomerList(bdCustomer);
        if (StringUtils.isEmpty(bdCustomers)) {
            stringBuffer.append("[没有找到内部公司编码为：" + invoiceUnitNumber + "的客户]");
            return 0;
        }
        bdCustomer = bdCustomers.get(0);
        //设置送货客户
        omsSaleissuebill.setCustomernumber(bdCustomer.getCustomernumber());
        omsSaleissuebill.setCustomername(bdCustomer.getCustomername());
        //记账公司
        omsSaleissuebill.setAccountcompanynumber(inventoryUnit.getOrgnumber());
        omsSaleissuebill.setAccountcompanyname(inventoryUnit.getOrgname());
        //成本中心
        omsSaleissuebill.setCostcenternumber("1.91");
        omsSaleissuebill.setCostcentername("关联单位");
        //币别
//        omsSaleissuebill.setCurrency("CNY");
        //币别修改获取逻辑
        omsSaleissuebill.setCurrency(receiptbill.getCurrency());
        //汇率
        omsSaleissuebill.setExchangerate(new BigDecimal(1));
        //厂编
        omsSaleissuebill.setFactorymake(receiptbill.getFactorymake());
        //来源单据
        omsSaleissuebill.setSrcbilltype("收货确认");
        omsSaleissuebill.setSrcbillnumber(receiptbill.getCode());
        //备注
        omsSaleissuebill.setDescription(receiptbill.getBuyernote());
        //创建时间
        omsSaleissuebill.setCreatetime(new Date());
        //是否外部客户
        omsSaleissuebill.setIsoutcustomer(0l);


        /*设置采入单单据头数据*/


        //设置业务日期
        omsPurinwarehsbill.setBizdate(receiptbill.getReceiptdate());
        String PWbillNumber = setBillNumber(omsPurinwarehsbill);
        //设置单据编号
        omsPurinwarehsbill.setBillnumber(PWbillNumber);
        //设置库存组织
        omsPurinwarehsbill.setStorageorgnumber(invoiceUnitNumber);
        omsPurinwarehsbill.setStorageorgname(invoiceUnit.getOrgname());
        //设置业务类型
        omsPurinwarehsbill.setBiztypenumber("110");
        omsPurinwarehsbill.setBiztypename("普通采购");
        //设置事务类型
        omsPurinwarehsbill.setTransactiontypenumber("004");
        omsPurinwarehsbill.setTransactiontypename("成本入库");
        //获取发货组织对应的供应商
        String inventoryNumber = inventoryUnit.getOrgnumber();
        BdSupplier bdSupplier = new BdSupplier();
        bdSupplier.setInternalcompanynumber(inventoryNumber);
        List<BdSupplier> bdSuppliers = supplierService.selectBdSupplierList(bdSupplier);
        if (StringUtils.isEmpty(bdSuppliers)) {
            stringBuffer.append("[没有找到内部公司编码为：" + inventoryNumber + "的供应商]");
            return 0;
        }
        bdSupplier = bdSuppliers.get(0);
        //设置供应商
        omsPurinwarehsbill.setSuppliernumber(bdSupplier.getSuppliernumber());
        omsPurinwarehsbill.setSuppliername(bdSupplier.getSuppliername());
        //设置成本中心
        omsPurinwarehsbill.setCostcenternumber("1.91");
        omsPurinwarehsbill.setCostcentername("关联单位");
        //设置记账公司
        omsPurinwarehsbill.setAccountcompanynumber(invoiceUnitNumber);
        omsPurinwarehsbill.setAccountcompanyname(invoiceUnit.getOrgname());
        //币别
//        omsPurinwarehsbill.setCurrency("CNY");
        //币别修改获取逻辑
        omsPurinwarehsbill.setCurrency(receiptbill.getCurrency());
        //汇率
        omsPurinwarehsbill.setExchangerate(BigDecimal.valueOf(1));
        //来源单据
        omsPurinwarehsbill.setSrcbilltype("收货确认");
        omsPurinwarehsbill.setSrcbillnumber(receiptbill.getCode());
        //备注
        omsPurinwarehsbill.setDescription(receiptbill.getBuyernote());
        //创建时间
        omsPurinwarehsbill.setCreatetime(new Date());



        //设置分录数据
        ArrayList<OmsSaleissuebillentry> saleissuebillentries = new ArrayList<>();
        ArrayList<OmsPurinwarehsbillentry> purinwarehsbillentries = new ArrayList<>();
        //获取销售出库单的发货仓库
        String wareHouse;
        String wareHouseName;
        //获取单据头的库存组织
        String billHeadInventoryOrg = receiptbill.getInventoryorganizationcode();
        if (billHeadInventoryOrg.equals(inventoryNumber) && billHeadInventoryOrg.equals(invoiceUnit.getFirstorgnumber())){
            String facilitycode = receiptbill.getFacilitycode();
            BdWarehouse bdWarehouse = new BdWarehouse();
            bdWarehouse.setWarehousenumber(facilitycode);
            List<BdWarehouse> bdWarehouses = warehouseService.selectBdWarehouseList(bdWarehouse);
            bdWarehouse = bdWarehouses.get(0);
            String transferwhnumber = bdWarehouse.getTransferwhnumber();
            if (StringUtils.isEmpty(transferwhnumber)) {
                wareHouse = facilitycode;
                wareHouseName = receiptbill.getFacilityname();
//                stringBuffer.append("[仓库：" + facilitycode + " 的在途仓库为空！]");
//                return 0;
            }else {
                wareHouse = transferwhnumber;
                wareHouseName = bdWarehouse.getTransferwhname();
            }

        } else {
            String transferwhnumber = inventoryUnit.getTransferwhnumber();
            if (StringUtils.isEmpty(transferwhnumber)) {
                stringBuffer.append("[组织：" + inventoryNumber + " 的中转仓为空！]");
                return 0;
            }
            wareHouse = transferwhnumber;
            wareHouseName = inventoryUnit.getTransferwhname();
        }

        String pWWareHouseNumber;
        String pWWareHouseName;
        //获取采购入库单的仓库数据
        //直接获取单据头storageOrgNumber的中转仓
        String transferwhnumber = invoiceUnit.getTransferwhnumber();
        if (StringUtils.isEmpty(transferwhnumber)) {
            stringBuffer.append("[组织："+ invoiceUnitNumber +"的中转仓为空！]");
            return 0;
        }
        pWWareHouseNumber = transferwhnumber;
        pWWareHouseName = invoiceUnit.getTransferwhname();


        for (OmsReceiptbillentry omsReceiptbillentry : receiptbill.getOmsReceiptbillentryList()) {
            /*设置销出单分录数据*/


            OmsSaleissuebillentry omsSaleissuebillentry = new OmsSaleissuebillentry();
            //仓库
            omsSaleissuebillentry.setWarehousenumber(wareHouse);
            omsSaleissuebillentry.setWarehousename(wareHouseName);
            //物料
            String skucode = omsReceiptbillentry.getSkucode();
            String skuname = omsReceiptbillentry.getSkuname();
            omsSaleissuebillentry.setMaterialnumber(skucode);
            omsSaleissuebillentry.setMaterialname(skuname);
            //计量单位
            String unit = omsReceiptbillentry.getUnit();
            omsSaleissuebillentry.setUnitnumber(unit);
            //基本计量单位
            String baseunit = omsReceiptbillentry.getBaseunit();
            omsSaleissuebillentry.setBaseunitnumber(baseunit);
            //数量
            BigDecimal qty = omsReceiptbillentry.getReceivedqty();
            omsSaleissuebillentry.setQty(qty);
            //基本单位数量
            BigDecimal baseQty = omsReceiptbillentry.getBasereceivedqty();
            omsSaleissuebillentry.setBaseqty(baseQty);
            //系统含税单价
            BigDecimal systaxprice = omsReceiptbillentry.getSystaxprice();
            omsSaleissuebillentry.setSystaxprice(systaxprice);
            //含税单价
            BigDecimal taxPrice = getTaxPrice(inventoryUnit, omsSaleissuebill.getCustomernumber(), omsSaleissuebill.getBizdate(), omsSaleissuebillentry.getMaterialnumber(), stringBuffer);
            if (StringUtils.isNull(taxPrice)) {
                return 0;
            }
            omsSaleissuebillentry.setTaxprice(taxPrice);
            //税率
            BigDecimal tempTaxrate = omsReceiptbillentry.getTaxrate();
            omsSaleissuebillentry.setTaxrate(tempTaxrate);
            BigDecimal taxrate = tempTaxrate;
            if (StringUtils.isNull(tempTaxrate)) {
                taxrate = BigDecimal.ZERO;
            }
            BigDecimal tempTaxRate = OmsTaskUtil.formatTaxRate(taxrate);
            //单价
            BigDecimal price = taxPrice.divide(tempTaxRate.add(BigDecimal.valueOf(1)),6, BigDecimal.ROUND_HALF_UP);
            omsSaleissuebillentry.setPrice(price);
            //税价合计
            BigDecimal taxAmount = taxPrice.multiply(qty).setScale(6,RoundingMode.HALF_UP);
            omsSaleissuebillentry.setTaxamount(taxAmount);
            //金额
//            BigDecimal amount = taxAmount.divide(tempTaxRate.add(BigDecimal.valueOf(1)),2,BigDecimal.ROUND_HALF_UP);
            BigDecimal amount = price.multiply(qty).setScale(2,RoundingMode.HALF_UP);
            omsSaleissuebillentry.setAmount(amount);
            //税额
            BigDecimal tax = taxAmount.subtract(amount);
            omsSaleissuebillentry.setTax(tax);
            //是否赠品
            //内部销售没有赠品!
            Long isgift = omsReceiptbillentry.getIsgift();
            omsSaleissuebillentry.setIspresent(0L);
            //客户订单号
            omsSaleissuebillentry.setCustomerOrderNo(omsReceiptbillentry.getCustomerorder());

            saleissuebillentries.add(omsSaleissuebillentry);


            /*设置采入单分录数据*/
            OmsPurinwarehsbillentry omsPurinwarehsbillentry = new OmsPurinwarehsbillentry();
            //仓库
            omsPurinwarehsbillentry.setWarehousenumber(pWWareHouseNumber);
            omsPurinwarehsbillentry.setWarehousename(pWWareHouseName);
            //物料
            omsPurinwarehsbillentry.setMaterialnumber(skucode);
            omsPurinwarehsbillentry.setMaterialname(skuname);
            //计量单位
            omsPurinwarehsbillentry.setUnitnumber(unit);
            omsPurinwarehsbillentry.setBaseunitnumber(baseunit);
            //数量
            omsPurinwarehsbillentry.setQty(qty);
            omsPurinwarehsbillentry.setBaseqty(baseQty);
            //含税单价
            omsPurinwarehsbillentry.setTaxprice(taxPrice);
            //税率
            omsPurinwarehsbillentry.setTaxrate(tempTaxrate);
            //单价
            omsPurinwarehsbillentry.setPrice(price);
            //税价合计
            omsPurinwarehsbillentry.setTaxamount(taxAmount);
            //金额
            omsPurinwarehsbillentry.setAmount(amount);
            //税额
            omsPurinwarehsbillentry.setTax(tax);
            //是否赠品
            //内部销售没有赠品!
            omsPurinwarehsbillentry.setIspresent(0L);

            purinwarehsbillentries.add(omsPurinwarehsbillentry);
        }
        omsSaleissuebill.setOmsSaleissuebillentryList(saleissuebillentries);
        omsPurinwarehsbill.setOmsPurinwarehsbillentryList(purinwarehsbillentries);

        int i = saleissuebillService.insertOmsSaleissuebill(omsSaleissuebill);
        if (i == 0) {
            stringBuffer.append("[插入销出单时出现错误！]");
            return 0;
        }
        int i1 = purinwarehsbillService.insertOmsPurinwarehsbill(omsPurinwarehsbill);
        if (i1 == 0) {
            stringBuffer.append("[插入采购入库单时出现错误！]");
            return 0;
        }

        return 1;

    }

    /**
     * 传入销售组织、销售组织对应的内部客户、收货确认单
     * 生成一张调拨单
     * @param invoiceUnit
     * @param bdCustomer
     * @param receiptbill
     * @param stringBuffer
     * @return
     */
    public int createInvoiceUnitTransferBill(OmsOrgunit invoiceUnit, BdCustomer bdCustomer, OmsReceiptbill receiptbill, StringBuffer stringBuffer){
        OmsStocktransferbill omsStocktransferbill = new OmsStocktransferbill();
        // 开始设置单据头数据

        //业务日期
        omsStocktransferbill.setBizdate(receiptbill.getReceiptdate());
        omsStocktransferbill.setBizdate(receiptbill.getReceiptdate());
        //单据编号
        String billNumber = setBillNumber(omsStocktransferbill);
        omsStocktransferbill.setBillnumber(billNumber);
        //业务类型
        omsStocktransferbill.setBiztypenumber("331");
        omsStocktransferbill.setBiztypename("跨仓库调拨（成本价）");
        //获取库存组织
        String orgnumber = invoiceUnit.getOrgnumber();
        String orgname = invoiceUnit.getOrgname();
        //调出仓库组织
        omsStocktransferbill.setIssuestorageorgnumber(orgnumber);
        omsStocktransferbill.setIssuestorageorgname(orgname);
        //调入库存组织
        omsStocktransferbill.setReceiptstorageorgnumber(orgnumber);
        omsStocktransferbill.setReceiptstorageorgname(orgname);
        //调出成本中心
        omsStocktransferbill.setOutcostcenternumber(receiptbill.getCostcentercode());
        omsStocktransferbill.setOutcostcentername(receiptbill.getCostcentername());
        //调出记账公司
        omsStocktransferbill.setOutaccountcompanynumber(orgnumber);
        omsStocktransferbill.setOutaccountcompanyname(orgname);
        //调入成本中心
        String costcenternumber = bdCustomer.getCostcenternumber();
//        if (StringUtils.isEmpty(costcenternumber)) {
//            stringBuffer.append("[客户：" + bdCustomer.getCustomernumber() + " 的成本中心为空！]");
//            return 0;
//        }
        omsStocktransferbill.setIncostcenternumber(costcenternumber);
        omsStocktransferbill.setIncostcentername(bdCustomer.getCostcentername());
        //调入记账公司
        omsStocktransferbill.setInaccountcompanynumber(orgnumber);
        omsStocktransferbill.setInaccountcompanyname(orgname);
        //币别
        omsStocktransferbill.setCurrency(receiptbill.getCurrency());
        //汇率
        omsStocktransferbill.setExchangerate(receiptbill.getExchangerate());
        //来源单据
        omsStocktransferbill.setSrcbilltype("收货确认");
        omsStocktransferbill.setSrcbillnumber(receiptbill.getCode());
        //备注
        omsStocktransferbill.setDescription(receiptbill.getBuyernote());
        //创建时间
        omsStocktransferbill.setCreatetime(new Date());

        //设置分录数据
        ArrayList<OmsStocktransferbillentry> stocktransferbillentries = new ArrayList<>();
        for (OmsReceiptbillentry omsReceiptbillentry : receiptbill.getOmsReceiptbillentryList()) {
            OmsStocktransferbillentry omsStocktransferbillentry = new OmsStocktransferbillentry();
            //物料
            omsStocktransferbillentry.setMaterialnumber(omsReceiptbillentry.getSkucode());
            omsStocktransferbillentry.setMaterialname(omsReceiptbillentry.getSkuname());
            //调出仓库
            omsStocktransferbillentry.setIssuewarehousenumber(invoiceUnit.getTransferwhnumber());
            omsStocktransferbillentry.setIssuewarehousename(invoiceUnit.getTransferwhname());
            //调入仓库
            omsStocktransferbillentry.setReceiptwarehousenumber(receiptbill.getReceivefacilitycode());
            omsStocktransferbillentry.setReceiptwarehousename(receiptbill.getReceivefacilityname());
            //计量单位
            omsStocktransferbillentry.setUnitnumber(omsReceiptbillentry.getUnit());
            //基本计量单位
            omsStocktransferbillentry.setBaseunitnumber(omsReceiptbillentry.getBaseunit());
            //数量
            omsStocktransferbillentry.setQty(omsReceiptbillentry.getReceivedqty());
            //基本单位数量
            omsStocktransferbillentry.setBaseqty(omsReceiptbillentry.getBasereceivedqty());
            //计划调出日期
            omsStocktransferbillentry.setIssueplandate(receiptbill.getReceiptdate());
            //计划调入日期
            omsStocktransferbillentry.setReceiptplandate(receiptbill.getReceiptdate());
            //赠品
            omsStocktransferbillentry.setIspresent(omsReceiptbillentry.getIsgift());

            stocktransferbillentries.add(omsStocktransferbillentry);
        }
        omsStocktransferbill.setOmsStocktransferbillentryList(stocktransferbillentries);
        int i = stocktransferbillService.insertOmsStocktransferbill(omsStocktransferbill);
        if (i == 0) {
            stringBuffer.append("[插入库存调拨单时出现错误！]");
            return 0;
        }
        return 1;
    }

    /**
     * ！！！废弃！！！
     * 传入销售组织，收货组织，物料编码，获取含税单价
     * @param outOrgUnit 销售组织
     * @param customerNumber 收货组织对应的客户编码
     * @param bizdate 结算日期
     * @param materialNumber 物料编码
     * @param stringBuffer   错误信息
     * @return 含税单价
     */
    @Deprecated
    public BigDecimal getTaxPriceDelete(OmsOrgunit outOrgUnit, String customerNumber, Date bizdate, String materialNumber, StringBuffer stringBuffer) {
        //先获取结算价格
        if (1 == 1){
            return new BigDecimal(1);
        }

        String orgnumber = outOrgUnit.getOrgnumber();
        BigDecimal settlementPrice = omsTaskutilMapper.getSettlementPrice(orgnumber, bizdate, materialNumber);
        if (StringUtils.isNull(settlementPrice)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            stringBuffer.append("没有找到对应的销售组织为 " + orgnumber + " 业务日期为 " + format.format(bizdate) + " 物料编码为 " + materialNumber + "且已审核的结算价格数据");
            return null;
        }
        //获取结算加成比率
        BigDecimal rate = new BigDecimal(0);
        BdMaterial bdMaterial = new BdMaterial();
        bdMaterial.setMaterialnumber(materialNumber);
        List<BdMaterial> bdMaterials = materialService.selectBdMaterialList(bdMaterial);
        bdMaterial = bdMaterials.get(0);
        String group3number = bdMaterial.getGroup3number();
        String group2number = bdMaterial.getGroup2number();
        String group1number = bdMaterial.getGroup1number();
        if (StringUtils.isNotEmpty(group3number)) {
            BigDecimal tempRate3 = omsTaskutilMapper.getSettlementRate3(orgnumber, bizdate, customerNumber, group3number);
            if (StringUtils.isNotNull(tempRate3)) {
                rate = tempRate3;
            } else if (StringUtils.isNotEmpty(group2number)) {
                BigDecimal tempRate2 = omsTaskutilMapper.getSettlementRate2(orgnumber, bizdate, customerNumber, group2number);
                if (StringUtils.isNotNull(tempRate2)) {
                    rate = tempRate2;
                } else if (StringUtils.isNotEmpty(group1number)) {
                    BigDecimal tempRate1 = omsTaskutilMapper.getSettlementRate1(orgnumber, bizdate, customerNumber, group1number);
                    if (StringUtils.isNotNull(tempRate1)) {
                        rate = tempRate1;
                    }
                }
            } else if (StringUtils.isNotEmpty(group1number)) {
                BigDecimal tempRate1 = omsTaskutilMapper.getSettlementRate1(orgnumber, bizdate, customerNumber, group1number);
                if (StringUtils.isNotNull(tempRate1)) {
                    rate = tempRate1;
                }
            }
        } else if (StringUtils.isNotEmpty(group2number)) {
            BigDecimal tempRate2 = omsTaskutilMapper.getSettlementRate2(orgnumber, bizdate, customerNumber, group2number);
            if (StringUtils.isNotNull(tempRate2)) {
                rate = tempRate2;
            } else if (StringUtils.isNotEmpty(group1number)) {
                BigDecimal tempRate1 = omsTaskutilMapper.getSettlementRate1(orgnumber, bizdate, customerNumber, group1number);
                if (StringUtils.isNotNull(tempRate1)) {
                    rate = tempRate1;
                }
            }
        } else if (StringUtils.isNotEmpty(group1number)) {
            BigDecimal tempRate1 = omsTaskutilMapper.getSettlementRate1(orgnumber, bizdate, customerNumber, group1number);
            if (StringUtils.isNotNull(tempRate1)) {
                rate = tempRate1;
            }
        }


        return settlementPrice.multiply(rate.add(BigDecimal.valueOf(1)));
    }

    /**
     * 传入两个结算组织编码
     * 返回距离这两个组织的最近根节点
     * 如果没有就返回null
     * @param org1 组织1
     * @param org2 组织2
     * @return 为空说明没有节点
     */
    public OmsOrgunit getOrgNode(OmsOrgunit org1, OmsOrgunit org2) {
        String longnumber1 = org1.getLongnumber();
        String longnumber2 = org2.getLongnumber();
        String[] split1 = longnumber1.split("!");
        String[] split2 = longnumber2.split("!");
        for (int i = split1.length-1; i >= 0; i--) {
            for (int j = split2.length-1; j >= 0; j--) {
                if (split1[i].equals(split2[j])) {
                    OmsOrgunit omsOrgunit = new OmsOrgunit();
                    omsOrgunit.setOrgnumber(split1[i]);
                    List<OmsOrgunit> omsOrgunits = orgunitService.selectOmsOrgunitList(omsOrgunit);
                    return omsOrgunits.get(0);
                }
            }
        }
        return null;
    }


//    public BigDecimal getTaxPrice(OmsOrgunit saleOrg,String customerNumber, Date bizdate, String materialNumber, StringBuffer stringBuffer){
//        BigDecimal noScaleTaxPrice = noScaleTaxPrice(saleOrg, customerNumber, bizdate, materialNumber, stringBuffer);
//        if (StringUtils.isNull(noScaleTaxPrice)){
//            return null;
//        }
//        //判断结算策略价格分录的比例是否为空
//        //如果不为空，乘上比例，为空忽略
//        BdCustomer bdCustomer = new BdCustomer();
//        bdCustomer.setCustomernumber(customerNumber);
//        List<BdCustomer> bdCustomers = customerService.selectBdCustomerList(bdCustomer);
//        bdCustomer = bdCustomers.get(0);
//        String customerUnitNumber = bdCustomer.getInternalcompanynumber();
//        String saleorgnumber = bdCustomer.getSaleorgnumber();
//        //获取的客户对应组织
//        String customerUseOrgNumber;
//        if (StringUtils.isBlank(customerUnitNumber)){
//            customerUseOrgNumber = saleorgnumber;
//        }else {
//            customerUseOrgNumber = customerUnitNumber;
//        }
//
//        OmsOrgunit customerUnit = new OmsOrgunit();
//        customerUnit.setOrgnumber(customerUseOrgNumber);
//        List<OmsOrgunit> omsOrgunits = orgunitService.selectOmsOrgunitList(customerUnit);
//        customerUnit = omsOrgunits.get(0);
//
//        //获取价格策略价格分录对象
//        OmsSettlementrateentry taxPrice = getTaxPrice(saleOrg, customerUnit, materialNumber, bizdate, stringBuffer);
//        BigDecimal scale = taxPrice.getScale();
//        if (StringUtils.isNull(scale) || (BigDecimal.ZERO).equals(scale)){
//            return noScaleTaxPrice;
//        }else {
//            return noScaleTaxPrice.multiply(scale);
//        }
//
//    }




        //获取结算价格数据

    /**
     * 传入销售组织，客户对应内部公司，物料编码，业务日期
     * 如果返回值为 null ，则为出错！
     * @param saleOrg 销售组织
     * @param customerNumber 客户编码
     * @param materialNumber 物料编码
     * @param bizdate 业务日期
     * @param stringBuffer 存储错误信息
     * @return 内部结算价或null
     */
    public BigDecimal getTaxPrice(OmsOrgunit saleOrg,String customerNumber, Date bizdate, String materialNumber, StringBuffer stringBuffer){
        //暂存传入的StringBuffer数据
        String tempStringBuffer = stringBuffer.toString();
        BdCustomer bdCustomer = new BdCustomer();
        bdCustomer.setCustomernumber(customerNumber);
        List<BdCustomer> bdCustomers = customerService.selectBdCustomerList(bdCustomer);
        if (StringUtils.isEmpty(bdCustomers)){
            stringBuffer.append("[客户：" + customerNumber + "不存在]");
            return null;
        }
        bdCustomer = bdCustomers.get(0);
        String customerUnitNumber = bdCustomer.getInternalcompanynumber();
        String saleorgnumber = bdCustomer.getSaleorgnumber();
        if (StringUtils.isEmpty(customerUnitNumber)){
//            stringBuffer.append("[客户:" + customerNumber + "的内部公司编码为空]");
            //如果客户内部公司编码为空，则获取客户的销售组织编码
            if (StringUtils.isBlank(saleorgnumber)){
                stringBuffer.append("[客户:" + customerNumber + "的内部公司编码为空且销售组织编码为空]");
                return null;
            }
        }
        OmsOrgunit customerUnit = new OmsOrgunit();
        //获取的客户对应组织
        String customerUseOrgNumber;
        if (StringUtils.isBlank(customerUnitNumber)){
            customerUseOrgNumber = saleorgnumber;
        }else {
            customerUseOrgNumber = customerUnitNumber;
        }
        customerUnit.setOrgnumber(customerUseOrgNumber);
        List<OmsOrgunit> omsOrgunits = orgunitService.selectOmsOrgunitList(customerUnit);
        if (StringUtils.isEmpty(omsOrgunits)){
            stringBuffer.append("[组织：" + customerUseOrgNumber + "不存在]");
            return null;
        }
        customerUnit = omsOrgunits.get(0);
        BdMaterial bdMaterial = new BdMaterial();
        bdMaterial.setMaterialnumber(materialNumber);
        List<BdMaterial> bdMaterials = materialService.selectBdMaterialList(bdMaterial);
        if (StringUtils.isEmpty(bdMaterials)){
            stringBuffer.append("[物料：" + materialNumber + "不存在]");
            return null;
        }
        bdMaterial = bdMaterials.get(0);
        //获取价格策略价格分录对象
        //获取含税单价
        BigDecimal taxPrice = getTaxPriceAmount(saleOrg, customerUnit, bdMaterial, bizdate, stringBuffer);
        if (StringUtils.isNotNull(taxPrice)){
            stringBuffer.delete(0,stringBuffer.length());
            stringBuffer.append(tempStringBuffer);
        }
        return taxPrice;
    }



    private BigDecimal getTaxPriceAmount(OmsOrgunit saleOrg,OmsOrgunit customerUnit,BdMaterial bdMaterial, Date bizdate,StringBuffer stringBuffer){
        //获取物料编码
        String materialNumber = bdMaterial.getMaterialnumber();
        //销售组织编码
        String orgnumber = saleOrg.getOrgnumber();
        //采购组织编码
        String customerUnitOrgnumber = customerUnit.getOrgnumber();
        //获取手工维护数据--正向数据
        OmsSettlementrateentry settlementRateByHand = omsTaskutilMapper.getSettlementRateByHand(orgnumber, bizdate, customerUnitOrgnumber, materialNumber);
        if (StringUtils.isNotNull(settlementRateByHand)){
            BigDecimal taxPricetojudge = getTaxPricetojudge(bdMaterial, settlementRateByHand, bizdate, stringBuffer);
            if (StringUtils.isNotNull(taxPricetojudge)){
                return taxPricetojudge;
            }
        }
        //获取手工维护数据--反向获取
        OmsSettlementrateentry settlementRateByHand1 = omsTaskutilMapper.getSettlementRateByHand(customerUnitOrgnumber, bizdate, orgnumber, materialNumber);
        if (StringUtils.isNotNull(settlementRateByHand1)){
            BigDecimal taxPricetojudge = getTaxPricetojudge(bdMaterial, settlementRateByHand1, bizdate, stringBuffer);
            if (StringUtils.isNotNull(taxPricetojudge)){
                return taxPricetojudge;
            }
        }
        String group4number = bdMaterial.getGroup4number();
        String group3number = bdMaterial.getGroup3number();
        String group2number = bdMaterial.getGroup2number();
        String group1number = bdMaterial.getGroup1number();
        //获取四级分类数据--正向
        List<OmsSettlementrateentry> settlementRateByType4 = omsTaskutilMapper.getSettlementRateByType(orgnumber, bizdate, customerUnitOrgnumber, group4number);
        if (StringUtils.isNotEmpty(settlementRateByType4)){
            for (OmsSettlementrateentry omsSettlementrateentry : settlementRateByType4) {
                BigDecimal taxPricetojudge = getTaxPricetojudge(bdMaterial, omsSettlementrateentry, bizdate, stringBuffer);
                if (StringUtils.isNotNull(taxPricetojudge)){
                    return taxPricetojudge;
                }
            }
        }
        //获取四级分类数据--反向
        List<OmsSettlementrateentry> settlementRateByType41 = omsTaskutilMapper.getSettlementRateByType(customerUnitOrgnumber, bizdate, orgnumber, group4number);
        if (StringUtils.isNotEmpty(settlementRateByType41)){
            for (OmsSettlementrateentry omsSettlementrateentry : settlementRateByType41) {
                BigDecimal taxPricetojudge = getTaxPricetojudge(bdMaterial, omsSettlementrateentry, bizdate, stringBuffer);
                if (StringUtils.isNotNull(taxPricetojudge)){
                    return taxPricetojudge;
                }
            }
        }
        //获取三级分类数据--正向
        List<OmsSettlementrateentry> settlementRateByTypes3 = omsTaskutilMapper.getSettlementRateByType(orgnumber, bizdate, customerUnitOrgnumber, group3number);
        if (StringUtils.isNotEmpty(settlementRateByTypes3)){
            for (OmsSettlementrateentry omsSettlementrateentry : settlementRateByTypes3) {
                BigDecimal taxPricetojudge = getTaxPricetojudge(bdMaterial, omsSettlementrateentry, bizdate, stringBuffer);
                if (StringUtils.isNotNull(taxPricetojudge)){
                    return taxPricetojudge;
                }
            }

        }
        //获取三级分类数据--反向
        List<OmsSettlementrateentry> settlementRateByTypes31 = omsTaskutilMapper.getSettlementRateByType(customerUnitOrgnumber, bizdate, orgnumber, group3number);
        if (StringUtils.isNotEmpty(settlementRateByTypes31)){
            for (OmsSettlementrateentry omsSettlementrateentry : settlementRateByTypes31) {
                BigDecimal taxPricetojudge = getTaxPricetojudge(bdMaterial, omsSettlementrateentry, bizdate, stringBuffer);
                if (StringUtils.isNotNull(taxPricetojudge)){
                    return taxPricetojudge;
                }
            }
        }
        //获取二级分类数据--正向
        List<OmsSettlementrateentry> settlementRateByTypes2 = omsTaskutilMapper.getSettlementRateByType(orgnumber, bizdate, customerUnitOrgnumber, group2number);
        if (StringUtils.isNotEmpty(settlementRateByTypes2)){
            for (OmsSettlementrateentry omsSettlementrateentry : settlementRateByTypes2) {
                BigDecimal taxPricetojudge = getTaxPricetojudge(bdMaterial, omsSettlementrateentry, bizdate, stringBuffer);
                if (StringUtils.isNotNull(taxPricetojudge)){
                    return taxPricetojudge;
                }
            }
        }
        //获取二级分类数据--反向
        List<OmsSettlementrateentry> settlementRateByTypes21 = omsTaskutilMapper.getSettlementRateByType(customerUnitOrgnumber, bizdate, orgnumber, group2number);
        if (StringUtils.isNotEmpty(settlementRateByTypes21)){
            for (OmsSettlementrateentry omsSettlementrateentry : settlementRateByTypes21) {
                BigDecimal taxPricetojudge = getTaxPricetojudge(bdMaterial, omsSettlementrateentry, bizdate, stringBuffer);
                if (StringUtils.isNotNull(taxPricetojudge)){
                    return taxPricetojudge;
                }
            }

        }
        //获取一级分类数据--正向
        List<OmsSettlementrateentry> settlementRateByTypes1 = omsTaskutilMapper.getSettlementRateByType(orgnumber, bizdate, customerUnitOrgnumber, group1number);
        if (StringUtils.isNotEmpty(settlementRateByTypes1)){
            for (OmsSettlementrateentry omsSettlementrateentry : settlementRateByTypes1) {
                BigDecimal taxPricetojudge = getTaxPricetojudge(bdMaterial, omsSettlementrateentry, bizdate, stringBuffer);
                if (StringUtils.isNotNull(taxPricetojudge)){
                    return taxPricetojudge;
                }
            }
        }
        //获取一级分类数据--反向
        List<OmsSettlementrateentry> settlementRateByTypes11 = omsTaskutilMapper.getSettlementRateByType(customerUnitOrgnumber, bizdate, orgnumber, group1number);
        if (StringUtils.isNotEmpty(settlementRateByTypes11)){
            for (OmsSettlementrateentry omsSettlementrateentry : settlementRateByTypes11) {
                BigDecimal taxPricetojudge = getTaxPricetojudge(bdMaterial, omsSettlementrateentry, bizdate, stringBuffer);
                if (StringUtils.isNotNull(taxPricetojudge)){
                    return taxPricetojudge;
                }
            }
        }

//        //上面流程都走完，如果还是没有返回数据，需要报错！
        HashSet<String> materialTypeSet = new HashSet<>();
        materialTypeSet.add(group1number);
        materialTypeSet.add(group2number);
        materialTypeSet.add(group3number);
        materialTypeSet.add(group4number);
        String materialTypes = materialTypeSet.toString();
        if (stringBuffer.length() == 0){
            stringBuffer.append("[价格策略无数据：").append("根据销售（收货）组织：").append(orgnumber).append("、").append(customerUnitOrgnumber).append("和业务日期：").append(new SimpleDateFormat("yyyy-MM-dd").format(bizdate)).append("对物料编码:").append(materialNumber).append("或是物料分类：").append(materialTypes).append("进行检索在价格策略中没有找到数据]");
        }
//        stringBuffer.append("[价格策略中未找到物料：" + materialNumber + "相关数据]");
        return null;
    }


    /**
     * 根据物料、价格策略的价格分录数据、业务日期获取对应的价格类型的价格
     * 如果为空则说明无数据
     * @param bdMaterial 物料
     * @param taxPrice 价格策略的价格分录
     * @param bizdate 业务日期
     * @param stringBuffer 错误信息
     * @return 得出的
     */
    private BigDecimal getTaxPricetojudge(BdMaterial bdMaterial,OmsSettlementrateentry taxPrice,Date bizdate,StringBuffer stringBuffer){
        String pricetype = taxPrice.getPricetype();
        String materialNumber = bdMaterial.getMaterialnumber();
        BigDecimal computeScale = new BigDecimal(1);
        BigDecimal scale = taxPrice.getScale();
        if (StringUtils.isNotNull(scale) && !(BigDecimal.ZERO).equals(scale)){
            computeScale = scale;
        }
        //手工维护：7
        if (pricetype.equals("7")) {
            BigDecimal taxprice = taxPrice.getTaxprice();
            return taxprice.multiply(computeScale);
        }
        //采购价：5
        if (pricetype.equals("5")) {
            BigDecimal bigDecimalPurprice = getBigDecimalPurprice(bdMaterial, bizdate, stringBuffer);
            if (StringUtils.isNull(bigDecimalPurprice)) {
                return null;
            }
            return bigDecimalPurprice.multiply(computeScale);
        }
        //考核价：6
        if (pricetype.equals("6")) {
            String groupnumber = taxPrice.getGroupnumber();
            if (StringUtils.isEmpty(groupnumber)){
                stringBuffer.append("[物料编码为：" + materialNumber + "业务日期为" + new SimpleDateFormat("yyyy-MM-dd").format(bizdate) + "的结算价分录数据的客户分组为空]");
                return null;
            }
            BigDecimal checkoutPrice = getCheckoutPrice(bdMaterial,groupnumber, bizdate, stringBuffer);
            if (StringUtils.isNull(checkoutPrice)) {
                return null;
            }
            return checkoutPrice.multiply(computeScale);
        }
        //1）标准零售价、测算价、加盟商价、维护价
        //均从结算价表中获取，匹配物料编码、生效日期、生效日期、状态。
        //根据物料编码、业务日期获取结算价分录数据
        OmsSettlementpriceentry settlementpriceentry = omsTaskutilMapper.getSettlementPriceByMaterialAndBizdate(materialNumber, bizdate);
        if (StringUtils.isNull(settlementpriceentry)){
            stringBuffer.append("[根据物料编码：" + materialNumber + "、业务日期" + new SimpleDateFormat("yyyy-MM-dd").format(bizdate) + "没有找到已审核的结算价分录数据]");
            return null;
        }
        OmsSettlementprice omsSettlementprice = settlementpriceService.selectOmsSettlementpriceById(taxPrice.getParentid());
        //标准零售价：1
        if (pricetype.equals("1")){
            BigDecimal price = settlementpriceentry.getPrice();
            if (StringUtils.isNull(price)){
                stringBuffer.append("[名称为：" + omsSettlementprice.getPricetactics() + "的物料：" + materialNumber + "无标准零售价]");
                return null;
            }
            return price.multiply(computeScale);
        }
        //测算价：2
        if (pricetype.equals("2")){
            BigDecimal primecostprice = settlementpriceentry.getPrimecostprice();
            if (StringUtils.isNull(primecostprice)){
                stringBuffer.append("[名称为：" + omsSettlementprice.getPricetactics() + "的物料：" + materialNumber + "无测算价]");
                return null;
            }
            return primecostprice.multiply(computeScale);
        }
        //加盟商价：3
        if (pricetype.equals("3")){
            BigDecimal joinprice = settlementpriceentry.getJoinprice();
            if (StringUtils.isNull(joinprice)){
                stringBuffer.append("[名称为：" + omsSettlementprice.getPricetactics() + "的物料：" + materialNumber + "无加盟商价]");
                return null;
            }
            return joinprice.multiply(computeScale);
        }
        //维护价：4
        if (pricetype.equals("4")){
            BigDecimal maintainprice = settlementpriceentry.getMaintainprice();
            if (StringUtils.isNull(maintainprice)){
                stringBuffer.append("[名称为：" + omsSettlementprice.getPricetactics() + "的物料：" + materialNumber + "无维护价]");
                return null;
            }
            return maintainprice.multiply(computeScale);
        }
        return null;
    }

    /**
     * 获取采购价类型价格
     * @param bdMaterial 物料对象
     * @param bizdate 业务日期
     * @param stringBuffer 错误信息
     * @return 采购价数据
     */
    private BigDecimal getBigDecimalPurprice(BdMaterial bdMaterial,Date bizdate,StringBuffer stringBuffer){
        String materialNumber = bdMaterial.getMaterialnumber();
        BdPurprice purprice = getPurprice(materialNumber, bizdate);
        if (StringUtils.isNull(purprice)){
            stringBuffer.append("[物料编码：" + materialNumber + "采购价格未维护]");
            return null;
        }
        String baseunitnumber = bdMaterial.getBaseunitnumber();
        if (StringUtils.isEmpty(baseunitnumber)){
            stringBuffer.append("[物料" + materialNumber + "的基本计量单位为空！]");
            return null;
        }
        String purpriceUnitnumber = purprice.getUnitnumber();
        if (!baseunitnumber.equals(purpriceUnitnumber)){
            stringBuffer.append("[物料:" + materialNumber + "的采购价计量单位：“" + purpriceUnitnumber + "”与物料的基本量单位：”" + baseunitnumber + "“不同！]");
            return null;
        }
        Long istaxprice = purprice.getIstaxprice();
        BigDecimal price = purprice.getPrice();
        BigDecimal taxrate = purprice.getTaxrate();
        //含税为是
        boolean boolean1 = istaxprice.equals(1L);
        //含税为否
        boolean boolean2 = istaxprice.equals(0L);
        //税率为空
        boolean boolean3 = StringUtils.isNull(taxrate);
        boolean boolean4 = new BigDecimal(0).equals(taxrate);
        boolean boolean5 = boolean3 || boolean4;
        //含税为否且税率为空
        boolean boolean6 = boolean2 && boolean5;
        if (boolean1 || boolean6){
            return price;
        }
        if (StringUtils.isNotNull(taxrate) && !new BigDecimal(0).equals(taxrate)){
            BigDecimal multiply = price.multiply(new BigDecimal(1).add(OmsTaskUtil.formatTaxRate(taxrate)));
            return multiply;
        }
        stringBuffer.append("[物料编码：" + materialNumber + "采购价格异常]");
        return null;
    }

    /**
     * 获取采购价对象
     * @param materialNumber 物料编码
     * @param bizdate 业务日期
     * @return 采购价对象
     */
    private BdPurprice getPurprice(String materialNumber,Date bizdate){
        //取失效日期小于业务日期所在月份最后一期数据
        //有则返回数据
        BdPurprice lastExpireDatePurprice = omsTaskutilMapper.getLastExpireDatePurprice(materialNumber, bizdate);
        if (StringUtils.isNotNull(lastExpireDatePurprice)){
            return lastExpireDatePurprice;
        }
        //获取业务日期处于生效失效日期之间的数据
        BdPurprice purpriceByBizDate = omsTaskutilMapper.getPurpriceByBizDate(materialNumber, bizdate);
        if (StringUtils.isNotNull(purpriceByBizDate)){
            return purpriceByBizDate;
        }
        //获取失效日期最新的数据
        BdPurprice purpriceByLastExpireDate = omsTaskutilMapper.getPurpriceByLastExpireDate(materialNumber);
        if (StringUtils.isNotNull(purpriceByLastExpireDate)){
            return purpriceByLastExpireDate;
        }
        return null;
    }


    /**
     * 获取考核价金额
     * @param bdMaterial 物料对象
     * @param groupnumber 客户组编码
     * @param bizdate 业务日期
     * @param stringBuffer 错误信息
     * @return 考核价金额
     */
    private BigDecimal getCheckoutPrice(BdMaterial bdMaterial,String groupnumber,Date bizdate,StringBuffer stringBuffer){
        //根据物料编码、客户组编码、业务日期获取优先级最小的一个
        String materialnumber = bdMaterial.getMaterialnumber();
        BdCheckoutprice checkoutprice = omsTaskutilMapper.getCheckoutprice(materialnumber, groupnumber, bizdate);
        if (StringUtils.isNull(checkoutprice)){
            //获取物料编码、客户组编码、失效日期最新的、优先级数字最小的一个数据
            checkoutprice = omsTaskutilMapper.getCheckoutpriceByLastExpireDate(materialnumber,groupnumber);
            if (StringUtils.isNull(checkoutprice)){
                //判空，如果不为空则继续往后判断，如果为空则直接报错返回Null
                stringBuffer.append("[客户组编码：" + groupnumber + "物料：" + materialnumber + "的考核价未维护]");
                return null;
            }

        }
        String unitnumber = checkoutprice.getUnitnumber();
        String checkpriceunitnumber = bdMaterial.getBaseunitnumber();
        if (!checkpriceunitnumber.equals(unitnumber)){
            stringBuffer.append("[物料:" + materialnumber + "的考核价计量单位：“" + checkpriceunitnumber + "”与物料的基本计量单位：”" + unitnumber + "“不同！]");
            return null;
        }
        return checkoutprice.getPrice();
    }


    /**
     * 格式化税率
     * @param taxrate 传入的bigdecimal对象
     * @return 格式化好的税率数据
     */
    public static BigDecimal formatTaxRate(BigDecimal taxrate){
        if (StringUtils.isNull(taxrate)){
            return null;
        }
        if (taxrate.compareTo(new BigDecimal(1)) > -1){
            BigDecimal divide = taxrate.divide(new BigDecimal(100), 6, BigDecimal.ROUND_HALF_UP);
            return divide;
        }
        return taxrate;
    }


    /**
     * 获取一级组织
     * @return 一级组织
     */
    public OmsOrgunit getFirstOrg(String orgNumber){
        OmsOrgunit omsOrgunit = new OmsOrgunit();
        omsOrgunit.setOrgnumber(orgNumber);
        List<OmsOrgunit> omsOrgunits = orgunitService.selectOmsOrgunitList(omsOrgunit);
        if(StringUtils.isEmpty(omsOrgunits)){
            return null;
        }
        if(omsOrgunits.get(0).getLevel()==1L){
            return omsOrgunits.get(0);
        }
        OmsOrgunit omsOrgunit1 = new OmsOrgunit();
        omsOrgunit1.setOrgnumber(omsOrgunits.get(0).getFirstorgnumber());
        List<OmsOrgunit> omsOrgunits1 = orgunitService.selectOmsOrgunitList(omsOrgunit1);
        if(!StringUtils.isEmpty(omsOrgunits1)){
            return omsOrgunits1.get(0);
        }else{
            OmsOrgunit omsOrgunit2 = new OmsOrgunit();
            String longnumber = omsOrgunits.get(0).getLongnumber();
            String[] split = longnumber.split("!");
            if(StringUtils.isEmpty(split)){
                return null;
            }
            omsOrgunit2.setOrgnumber(split[0]);
            List<OmsOrgunit> omsOrgunits2 = orgunitService.selectOmsOrgunitList(omsOrgunit2);
            if(!StringUtils.isEmpty(omsOrgunits2)){
                return omsOrgunits2.get(0);
            }
        }
        return null;
    }


    /**
     * 用于汇总单的String类型数据的判断
     * @param s1 传入的String类型数据
     * @param s2 传入的String类型的数据
     * @return 判断结果
     */
    public static boolean juedeString(String s1, String s2){
        boolean result = false;
        if (StringUtils.isEmpty(s1) && StringUtils.isEmpty(s2)){
            result = true;
        }else if (StringUtils.isNotEmpty(s1) && StringUtils.isNotEmpty(s2)){
            result = s1.equals(s2);
        }
        return result;
    }

    /**
     * 用户汇总单日期类型的判断
     * @param date1 日期
     * @param date2 日期
     * @return 判断结果
     */
    public static boolean juedeDate(Date date1, Date date2){
        boolean result = false;
        if (StringUtils.isNull(date1) && StringUtils.isNull(date2)){
            result = true;
        }else if (StringUtils.isNotNull(date1) && StringUtils.isNotNull(date2)){
            result = date1.equals(date2);
        }
        return result;
    }

    /**
     * 汇总单判断BigDecimal类型的数据
     * @param bigDecimal1 BigDecimal
     * @param bigDecimal2 BigDecimal
     * @return 结果
     */
    public static boolean juedeBigDecimal(BigDecimal bigDecimal1, BigDecimal bigDecimal2){
        boolean result = false;
        if (StringUtils.isNull(bigDecimal1) && StringUtils.isNull(bigDecimal2)){
            result = true;
        }else if (StringUtils.isNotNull(bigDecimal1) && StringUtils.isNotNull(bigDecimal2)){
            result = bigDecimal1.equals(bigDecimal2);
        }
        return result;
    }


    public void setTempTaxPrice(){
        ArrayList<tempTaxPrice> tempTaxPrices;
            tempTaxPrices = omsTaskutilMapper.selectTempTaxPriceData();
            for (tempTaxPrice tempTaxPrice : tempTaxPrices) {
                Date bizDate = tempTaxPrice.getBizDate();
                String customerNumber = tempTaxPrice.getCustomerNumber();
                String materialNumber = tempTaxPrice.getMaterialNumber();
                String storageOrgNumber = tempTaxPrice.getStorageOrgNumber();
                BigDecimal taxprice = tempTaxPrice.getTaxprice();
                OmsOrgunit omsOrgunit = orgunitService.selectOmsOrgunitByNumber(storageOrgNumber);
                StringBuffer stringBuffer = new StringBuffer();
                BigDecimal taxPrice = getTaxPrice(omsOrgunit, customerNumber, bizDate, materialNumber, stringBuffer);
                if (StringUtils.isNotNull(taxPrice)){
                    tempTaxPrice.setNewtaxprice(taxPrice);
                    omsTaskutilMapper.updateTempTaxPrice(tempTaxPrice);
                }
            }

    }


    public void testTempTaxPrice(String storageOrgNumber,String customerNumber,String bizDate,String materialNumber,String texprice){
        StringBuffer stringBuffer = new StringBuffer();
        OmsOrgunit omsOrgunit = orgunitService.selectOmsOrgunitByNumber(storageOrgNumber);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse;
        try {
            parse = simpleDateFormat.parse(bizDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        BigDecimal bigDecimal = new BigDecimal(texprice);
        tempTaxPrice tempTaxPrice = new tempTaxPrice();
        tempTaxPrice.setBizDate(parse);
        tempTaxPrice.setStorageOrgNumber(storageOrgNumber);
        tempTaxPrice.setCustomerNumber(customerNumber);
        tempTaxPrice.setMaterialNumber(materialNumber);
        tempTaxPrice.setTaxprice(bigDecimal);
        BigDecimal taxPrice = getTaxPrice(omsOrgunit, customerNumber, parse, materialNumber, stringBuffer);
        if (StringUtils.isNotNull(taxPrice))
            tempTaxPrice.setNewtaxprice(taxPrice);
        System.out.println(taxPrice);
        omsTaskutilMapper.updateTempTaxPrice(tempTaxPrice);
        System.out.println(stringBuffer);
    }

    //格式化传入的旺店通数据
    public static void formatWdtData(String pageNo, String pageSize, String startDate, String endDate, String shopNo, QMBillConfig qmBillConfig) throws ParseException {
        int truePageNo;
        if (StringUtils.isNotEmpty(pageNo)){
            truePageNo = Integer.parseInt(pageNo);
        }else {
            truePageNo = 1;
        }
        qmBillConfig.setPageNo(truePageNo);
        int truePageSize;
        if (StringUtils.isNotEmpty(pageSize)){
            truePageSize = Integer.parseInt(pageSize);
        }else {
            truePageSize = 100;
        }
        qmBillConfig.setPageSize(truePageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate startLocalDate;
        if (StringUtils.isNotEmpty(startDate)){
            Date start = dateFormat.parse(startDate);
            Instant startInstant = start.toInstant();
            startLocalDate = startInstant.atZone(zoneId).toLocalDate();
        }else {
            startLocalDate = LocalDate.now().minusDays(1);

        }
        qmBillConfig.setStartTime(startLocalDate);
        LocalDate endLocalDate;
        if (StringUtils.isNotEmpty(endDate)){
            Date end = dateFormat.parse(endDate);
            Instant endInstant = end.toInstant();
            endLocalDate = endInstant.atZone(zoneId).toLocalDate();
        }else {
            endLocalDate = LocalDate.now().minusDays(1);
        }
        qmBillConfig.setEndTime(endLocalDate);
        String trueShopNo;
        if (StringUtils.isNotEmpty(shopNo)){
            trueShopNo = shopNo;
        }else {
            trueShopNo = "";
        }
        qmBillConfig.setShopNo(trueShopNo);

    }

    public void dealAllRepeatBill(){
        dealRepeatReceBill();
        dealRepeatDeliverbill();
        dealRepeatSalereturnbill();
        dealRepeatTransferbill();
    }

    /**
     * 通过该方法，将当前月份开始的数据，进行去重，如果找到重复单据
     * 则会将其中，除一张外的全部单据设置为无需下发，并且写明错误信息。
     */
    public void dealRepeatReceBill(){
        //首先获取当前月份，创建一个时间节点从第一天开始，用以过滤数据
        Date time = getNowMonthDate();
        //根据当前时间节点，获取重复单据的编码，以及数量（大于一条）
        List<Map> repeatReceBill = omsTaskutilMapper.getRepeatReceBill(time);
        if (null == repeatReceBill || repeatReceBill.size() == 0){
            return;
        }
        for (Map map : repeatReceBill) {
            String code = (String) map.get("code");
            Long count = (Long) map.get("count");
            omsTaskutilMapper.updateRepeatReceBill(code, (int) (count - 1));
        }
    }
    /**
     * 通过该方法，将当前月份开始的数据，进行去重，如果找到重复单据
     * 则会将其中，除一张外的全部单据设置为无需下发，并且写明错误信息。
     */
    public void dealRepeatDeliverbill(){
        //首先获取当前月份，创建一个时间节点从第一天开始，用以过滤数据
        Date time = getNowMonthDate();
        //根据当前时间节点，获取重复单据的编码，以及数量（大于一条）
        List<Map> repeatReceBill = omsTaskutilMapper.getRepeatDeliverbill(time);
        if (null == repeatReceBill || repeatReceBill.size() == 0){
            return;
        }
        for (Map map : repeatReceBill) {
            String code = (String) map.get("code");
            Long count = (Long) map.get("count");
            omsTaskutilMapper.updateRepeatDeliverbill(code, (int) (count - 1));
        }
    }
    /**
     * 通过该方法，将当前月份开始的数据，进行去重，如果找到重复单据
     * 则会将其中，除一张外的全部单据设置为无需下发，并且写明错误信息。
     */
    public void dealRepeatSalereturnbill(){
        //首先获取当前月份，创建一个时间节点从第一天开始，用以过滤数据
        Date time = getNowMonthDate();
        //根据当前时间节点，获取重复单据的编码，以及数量（大于一条）
        List<Map> repeatReceBill = omsTaskutilMapper.getRepeatSalereturnbill(time);
        if (null == repeatReceBill || repeatReceBill.size() == 0){
            return;
        }
        for (Map map : repeatReceBill) {
            String code = (String) map.get("code");
            Long count = (Long) map.get("count");
            omsTaskutilMapper.updateRepeatSalereturnbill(code, (int) (count - 1));
        }
    }
    /**
     * 通过该方法，将当前月份开始的数据，进行去重，如果找到重复单据
     * 则会将其中，除一张外的全部单据设置为无需下发，并且写明错误信息。
     */
    public void dealRepeatTransferbill(){
        //首先获取当前月份，创建一个时间节点从第一天开始，用以过滤数据
        Date time = getNowMonthDate();
        //根据当前时间节点，获取重复单据的编码，以及数量（大于一条）
        List<Map> repeatReceBill = omsTaskutilMapper.getRepeatTransferbill(time);
        if (null == repeatReceBill || repeatReceBill.size() == 0){
            return;
        }
        for (Map map : repeatReceBill) {
            String code = (String) map.get("code");
            Long count = (Long) map.get("count");
            omsTaskutilMapper.updateRepeatTransferbill(code, (int) (count - 1));
        }
    }


    /**
     * 获取当前年月的第一日时间节点
     * @return
     */
    public Date getNowMonthDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date time = calendar.getTime();
        return time;
    }


    public List<String> selectRepeatBill(String tableNumber,Date date){
        List<String> strings = omsTaskutilMapper.selectRepeatBill(tableNumber, date);
        return strings;
    }


    /**
     * 删除对应的日志数据
     * @param deleteDate
     */

    public void deleteOperLog(Date deleteDate){
        omsTaskutilMapper.deleteOperLog(deleteDate);
    }

    public void deleteLoginLog(Date deleteDate){
        omsTaskutilMapper.deleteLoginLog(deleteDate);
    }

    public void deleteTaskLog(Date deleteDate){
        omsTaskutilMapper.deleteTaskLog(deleteDate);
    }


}
